package br.com.nooclix.ptt.ws.application;

import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import br.com.nooclix.ptt.core.application.SaleApplication;
import br.com.nooclix.ptt.core.domain.sale.Sale;
import br.com.nooclix.ptt.core.infrastructure.util.ApplicationContextRegisterCore;
import br.com.nooclix.ptt.core.infrastructure.util.ResponseData;
import br.com.nooclix.ptt.ws.adapter.Adapter;
import br.com.nooclix.ptt.ws.adapter.impl.SaleAdapter;
import br.com.nooclix.ptt.ws.utilities.Utilities;

@Path("/sale")
public class SaleRestApplication {

	private Adapter<Sale> adapter;
	private SaleApplication saleApplication;

	public SaleRestApplication() {
		super();
		adapter = new SaleAdapter();
		saleApplication = ApplicationContextRegisterCore.getBean(
				"saleApplication", SaleApplication.class);

	}

	@GET
	@Path("/find/sales/space/{spaceName}/{beginDate}/{endDate}/{offset}/{maxResult}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response findSalesBySpace(@PathParam("spaceName") String spaceName,
			@PathParam("beginDate") String beginStringDate,
			@PathParam("endDate") String endStringDate, @PathParam("offset") int offset, @PathParam("maxResult") int maxResult) {

		Response response = null;
		if (spaceName.equalsIgnoreCase("null")){
			spaceName = null;
		}
		Date beginDate = Utilities.getDateFromString(beginStringDate);
		Date endDate = Utilities.getDateFromString(endStringDate);
		ResponseData<Sale> salesResult = saleApplication.findSalesBySpace(spaceName, beginDate, endDate, offset, maxResult);
		System.out.println(salesResult.getItens().size());
		String json = adapter.convert(salesResult);
		response = Response.ok(json).build();
		return response;

	}
	
	public static void main(String[] args) {
		System.out.println(new SaleRestApplication().findSalesBySpace("null", "01-01-2007", "13-05-2013", 0,30).getEntity());
    }

}
